package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.DriverInfo;
import cn.wolfcode.business.mapper.DriverInfoMapper;
import cn.wolfcode.business.service.DriverInfoService;
import cn.wolfcode.business.utils.RegexUtils;
import cn.wolfcode.business.utils.VehiclePlateNoUtil;
import cn.wolfcode.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DriverInfoServiceImpl implements DriverInfoService {
    @Autowired
    private DriverInfoMapper driverInfoMapper;
    @Override
    public List<DriverInfo> selectDriverInfoList(DriverInfo driverInfo) {
        List<DriverInfo>list=driverInfoMapper.selectDriverInfoList(driverInfo);
        return list;
    }

    @Override
    public int insertDriverInfo(DriverInfo driverInfo) {
        //客户名称不能为空
        if ( driverInfo.getCustomerName()==null||driverInfo.getCustomerName().trim().isEmpty()){
            throw new RuntimeException("客户名称不能为空");
        }
        //判断手机号合法性
        if ( driverInfo.getCustomerContact()==null
            || driverInfo.getCustomerContact().isEmpty()
            ||(!RegexUtils.isPhoneLegal(driverInfo.getCustomerContact())) ){
            throw new RuntimeException("手机号不合法");
        }
        //试驾车型不能为空
        if ( driverInfo.getAppointmentModel()==null){
            throw new RuntimeException("试驾车型不能为空");
        }
        //判断预约时间合法性
        if ( driverInfo.getAppointmentDate()==null||driverInfo.getAppointmentDate().before(DateUtils.getNowDate())){
            throw new RuntimeException("预约时间不合法");
        }

        DriverInfo old=driverInfoMapper.selectByPhoneAndDate(driverInfo.getCustomerContact(),driverInfo.getAppointmentDate());
        if(old!=null) {
            if (old.getId() != driverInfo.getId()) {
                throw new RuntimeException("该用户当天已经预约");
            }
        }
        return driverInfoMapper.addDriverInfo(driverInfo);
    }

    @Override
    public DriverInfo selectDriverInfoById(Integer id) {
        return driverInfoMapper.selectDriverInfoById(id);
    }

    @Override
    public int updateDriverInfo(DriverInfo driverInfo) {

        //客户名称不能为空
        if ( driverInfo.getCustomerName()==null||driverInfo.getCustomerName().trim().isEmpty()){
            throw new RuntimeException("客户名称不能为空");
        }
        //判断手机号合法性
        if ( driverInfo.getCustomerContact()==null
                || driverInfo.getCustomerContact().isEmpty()
                ||(!RegexUtils.isPhoneLegal(driverInfo.getCustomerContact())) ){
            throw new RuntimeException("手机号不合法");
        }
        //试驾车型不能为空
        if ( driverInfo.getAppointmentModel()==null){
            throw new RuntimeException("试驾车型不能为空");
        }
        //判断预约时间合法性
        if ( driverInfo.getAppointmentDate()==null||driverInfo.getAppointmentDate().before(DateUtils.getNowDate())){
            throw new RuntimeException("预约时间不合法");
        }

        DriverInfo old=driverInfoMapper.selectByPhoneAndDate(driverInfo.getCustomerContact(),driverInfo.getAppointmentDate());
        if(old!=null) {
            if (old.getId() != driverInfo.getId()) {
                throw new RuntimeException("该用户当天已经预约");
            }
        }
        return driverInfoMapper.updateDriverInfo(driverInfo);
    }

    @Override
    public int deleteDriverInfoByIds(Integer id) {
        DriverInfo old=driverInfoMapper.selectDriverInfoById(id);
        if(old==null){
            throw new RuntimeException("非法参数");
        }
        return driverInfoMapper.deleteDriverInfoById(id);
    }

    @Override
    public int addfankui(DriverInfo driverInfo) {
        //车牌号不能为空
        if ( driverInfo.getLicensePlate()==null||driverInfo.getLicensePlate().isEmpty()){
            throw new RuntimeException("车牌号不能为空");
        }
        //判断车牌号合法性
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(driverInfo.getLicensePlate());
        if(vehiclePlateNo == null){
            throw new RuntimeException("车牌号码格式有误");
        }
        //
        DriverInfo old = selectDriverInfoById(driverInfo.getId());

        //开始时间不能为空
        if ( driverInfo.getActualStartTime()==null||driverInfo.getActualEndTime()==null){
            throw new RuntimeException("时间不能为空");
        }

        //开始时间小于结束时间
        if(!(driverInfo.getActualStartTime().before(driverInfo.getActualEndTime()))){
            throw new RuntimeException("开始时间要小于结束时间");
        }
//        //预约时间不能为空
//        if ( driverInfo.getAppointmentDate()==null){
//            throw new RuntimeException("预约时间不能为空");
//        }
        //预约时间小于开始时间
        if(!(old.getAppointmentDate().before(driverInfo.getActualStartTime()))){
            throw new RuntimeException("没到预约时间禁止试驾");
        }
        //结束时间小于当前时间
        if(!(driverInfo.getActualEndTime().before(DateUtils.getNowDate()))){
            throw new RuntimeException("试驾结束后才能反馈");
        }
        //评分不能为空
        if ( driverInfo.getCustomerRating()==null){
            throw new RuntimeException("评分不能为空");
        }

        //用户评分在1~5之间
        if(driverInfo.getCustomerRating()<1||driverInfo.getCustomerRating()>5){
            throw new RuntimeException("用户评分在1~5之间");
        }
        //用户反馈不能为空不能超过50字
        if(driverInfo.getCustomerFeedback()==null||driverInfo.getCustomerFeedback().isEmpty()||driverInfo.getCustomerFeedback().length()>50){
            throw new RuntimeException("用户反馈在0~50字之间");
        }


        return driverInfoMapper.addfankui(driverInfo);
    }
}
